package com.dinghui.mybatis.utils;//package com.dinghui.employment.utils;
//
//import org.apache.poi.hssf.usermodel.*;
//import org.apache.poi.ss.usermodel.HorizontalAlignment;
//import org.springframework.util.ResourceUtils;
//
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.io.*;
//import java.net.URLEncoder;
//import java.text.ParseException;
//import java.text.SimpleDateFormat;
//import java.util.Date;
//import java.util.List;
//import java.util.TimeZone;
//
//public class Exprot {
//
//    /**
//     * 导出根据条件查询出来的考勤记录
//     *
//     * @param searchParam
//     * @param list
//     * @param request
//     * @param response
//     * @throws Exception
//     */
//    public static void exportRecordSearch(RecordSearchParam searchParam, List<RecordSearchVo> list, HttpServletRequest request, HttpServletResponse response) throws Exception {
//
//        BufferedInputStream in = null;
//        BufferedOutputStream out = null;
//        //创建excel
//        HSSFWorkbook wb = new HSSFWorkbook();
//        String fileName = "考勤记录表.xls";//导出时下载的EXCEL文件名
//        //样式
//        HSSFCellStyle cellStyle = wb.createCellStyle();
//        cellStyle.setAlignment(HorizontalAlignment.CENTER_SELECTION); // 居中
//
//        //创建sheet
//        HSSFSheet sheet = wb.createSheet("考勤记录");
//        sheet.setDefaultColumnWidth(10);
//        sheet.setDefaultRowHeightInPoints(20);
//        sheet.setColumnWidth(0, 5000);
//        sheet.setColumnWidth(2, 9100);
//        sheet.setColumnWidth(3, 9100);
//        sheet.setColumnWidth(4, 6000);
//        sheet.setColumnWidth(5, 6000);
//        //创建条件行
//        HSSFRow conditionRow = sheet.createRow(0);
//        conditionRow.setHeightInPoints(22);
//        //创建条件列
//        HSSFCell conditionCell = conditionRow.createCell(0);
//        String un = searchParam.getUserName() == null ? "全部" : searchParam.getUserName();
//        conditionCell.setCellValue("查询用户：" + un);
//        conditionCell = conditionRow.createCell(1);
//        String cn = searchParam.getCourseName() == null ? "全部" : searchParam.getCourseName();
//        conditionCell.setCellValue("科目：" + cn);
//        conditionCell = conditionRow.createCell(2);
//        conditionCell.setCellValue("起始时间：" + getyyyyMMddDate(searchParam.getStartDate()));
//        conditionCell = conditionRow.createCell(3);
//        conditionCell.setCellValue("结束时间：" + getyyyyMMddDate(searchParam.getEndDate()));
//        conditionCell = conditionRow.createCell(4);
//
//        // 在行上创建表头列
//        HSSFRow rowTitle = sheet.createRow(1);
//        rowTitle.setHeightInPoints(22);
//        HSSFCell cellTitle = rowTitle.createCell(0);
//        cellTitle.setCellValue("用户名");
//        cellTitle = rowTitle.createCell(1);
//        cellTitle.setCellValue("课程名称");
//        cellTitle = rowTitle.createCell(2);
//        cellTitle.setCellValue("上课时间");
//        cellTitle = rowTitle.createCell(3);
//        cellTitle.setCellValue("下课时间");
//        cellTitle = rowTitle.createCell(4);
//        cellTitle.setCellValue("上课打卡时间");
//        cellTitle = rowTitle.createCell(5);
//        cellTitle.setCellValue("下课打卡时间");
//        cellTitle = rowTitle.createCell(6);
//        cellTitle.setCellValue("状态");
//
//        //从第三行开始放入对应的数据
//        for (int i = 0; i < list.size(); i++) {
//            RecordSearchVo record = list.get(i);
//            HSSFRow row = sheet.createRow(i + 2);
//            row.setHeightInPoints(20);
//            HSSFCell cell = row.createCell(0);
//            cell.setCellValue(record.getUserName());//用户名
//
//            cell = row.createCell(1);
//            cell.setCellValue(record.getCourseName());//课程名称
//
//            cell = row.createCell(2);
//            cell.setCellValue(getyyyyMMddDate(record.getCourseDate()) + " " + record.getStartTime());//上课时间
//
//            cell = row.createCell(3);
//            cell.setCellValue(getyyyyMMddDate(record.getCourseDate()) + " " + record.getEndTime());//下课时间
//
//            cell = row.createCell(4);
//            cell.setCellValue(record.getStartRecords().size() > 0 ? getyyyyMMddHHmmssDate(record.getStartRecords().get(0).getTime()) : "");//上课打卡时间
//
//            cell = row.createCell(5);
//            cell.setCellValue(record.getEndRecords().size() > 0 ? getyyyyMMddHHmmssDate(record.getEndRecords().get(0).getTime()) : "");//下课课打卡时间
//
//            cell = row.createCell(6);
//            cell.setCellValue(getClockStatus(record));//状态
//        }
//        String realPath = "C:/record.xls";
//        FileOutputStream fout = null;
//        fout = new FileOutputStream(realPath);
//        wb.write(fout);
//        fout.close();
//        wb.close();
//        try {
//            InputStream inputStream = new FileInputStream(ResourceUtils.getFile(realPath));
//            fileName = URLEncoder.encode(fileName, "UTF-8");
//            response.setContentType("applicationnd/octet-stream");
//            response.setCharacterEncoding("UTF-8");
//            response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
//            in = new BufferedInputStream(inputStream);
//            out = new BufferedOutputStream(response.getOutputStream());
//            byte[] data = new byte[1024];
//            int len = 0;
//            while (-1 != (len = in.read(data, 0, data.length))) {
//                out.write(data, 0, len);
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        } finally {
//            if (in != null) {
//                in.close();
//            }
//            if (out != null) {
//                out.close();
//            }
//            File f = new File(realPath);
//            if (f.exists()) {
//                f.delete();//删除
//            }
//        }
//    }
//
//    /**
//     * 按科目导出考勤统计
//     *
//     * @return
//     */
//    public static void exportStaticReport(RecordSearchParam searchParam, List<StaticReportVo> list, HttpServletRequest request, HttpServletResponse response) throws Exception {
//        BufferedInputStream in = null;
//        BufferedOutputStream out = null;
//        //创建excel
//        HSSFWorkbook wb = new HSSFWorkbook();
//        String fileName = "考勤统计.xls";//导出时下载的EXCEL文件名
//        //样式
//        HSSFCellStyle cellStyle = wb.createCellStyle();
//        cellStyle.setAlignment(HorizontalAlignment.CENTER_SELECTION); // 居中
//
//        //创建sheet
//        HSSFSheet sheet = wb.createSheet(searchParam.getWay() == 1 ? "按时间统计" : "按课程统计");
//        sheet.setDefaultColumnWidth(10);
//        sheet.setDefaultRowHeightInPoints(20);
//        sheet.setColumnWidth(0, 5000);
//        sheet.setColumnWidth(1, 5000);
//
//        //创建条件行
//        HSSFRow conditionRow = sheet.createRow(0);
//        conditionRow.setHeightInPoints(22);
//        //创建条件列
//        HSSFCell conditionCell = conditionRow.createCell(0);
//        if (searchParam.getWay() == 1) {
//            conditionCell.setCellValue("起始时间：" + getyyyyMMddDate(searchParam.getStartDate()));
//            conditionCell = conditionRow.createCell(1);
//            conditionCell.setCellValue("结束时间：" + getyyyyMMddDate(searchParam.getEndDate()));
//        } else {
//            String cn = searchParam.getCourseId() == null ? "全部" : searchParam.getCourseName();
//            conditionCell.setCellValue("查询科目：" + cn);
//        }
//
//        // 在行上创建表头列
//        HSSFRow rowTitle = sheet.createRow(1);
//        rowTitle.setHeightInPoints(22);
//        HSSFCell cellTitle = rowTitle.createCell(0);
//        cellTitle.setCellValue(searchParam.getWay() == 1 ? "课程名称" : "日期");
//        cellTitle = rowTitle.createCell(1);
//        cellTitle.setCellValue("总打卡数");
//        cellTitle = rowTitle.createCell(2);
//        cellTitle.setCellValue("正常打卡数");
//        cellTitle = rowTitle.createCell(3);
//        cellTitle.setCellValue("缺卡数");
//        cellTitle = rowTitle.createCell(4);
//        cellTitle.setCellValue("迟到卡数");
//        cellTitle = rowTitle.createCell(5);
//        cellTitle.setCellValue("早退卡数");
//
//        //数据行
//        for (int i = 0; i < list.size(); i++) {
//            StaticReportVo reportVo = list.get(i);
//            HSSFRow row = sheet.createRow(i + 2);
//            row.setHeightInPoints(20);
//            HSSFCell cell = row.createCell(0);
//            cell.setCellValue(searchParam.getWay() == 1 ? reportVo.getCourseName() : getyyyyMMddDate(reportVo.getCourseDate()));//课程名称 / 日期
//
//            cell = row.createCell(1);
//            cell.setCellValue(reportVo.getSignInNormalTotal() + reportVo.getSignInUnNormalTotal() + reportVo.getSignOutNormalTotal() + reportVo.getSignOutUnNormalTotal());//总打卡数
//
//            cell = row.createCell(2);
//            cell.setCellValue(reportVo.getSignInNormalTotal() + reportVo.getSignOutNormalTotal());//正常打卡数
//
//            cell = row.createCell(3);
//            cell.setCellValue(reportVo.getNeedSignInTotal() * 2 - (reportVo.getSignInNormalTotal() + reportVo.getSignInUnNormalTotal() + reportVo.getSignOutNormalTotal() + reportVo.getSignOutUnNormalTotal()));//缺卡数
//
//            cell = row.createCell(4);
//            cell.setCellValue(reportVo.getSignInUnNormalTotal());//迟到卡数
//
//            cell = row.createCell(5);
//            cell.setCellValue(reportVo.getSignOutUnNormalTotal());//早退卡数
//        }
//
//        String realPath = "C:/static_report.xls";
//        FileOutputStream fout = null;
//        fout = new FileOutputStream(realPath);
//        wb.write(fout);
//        fout.close();
//        wb.close();
//        try {
//            InputStream inputStream = new FileInputStream(ResourceUtils.getFile(realPath));
//            fileName = URLEncoder.encode(fileName, "UTF-8");
//            response.setContentType("applicationnd/octet-stream");
//            response.setCharacterEncoding("UTF-8");
//            response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
//            in = new BufferedInputStream(inputStream);
//            out = new BufferedOutputStream(response.getOutputStream());
//            byte[] data = new byte[1024];
//            int len = 0;
//            while (-1 != (len = in.read(data, 0, data.length))) {
//                out.write(data, 0, len);
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        } finally {
//            if (in != null) {
//                in.close();
//            }
//            if (out != null) {
//                out.close();
//            }
//            File f = new File(realPath);
//            if (f.exists()) {
//                f.delete();//删除
//            }
//        }
//    }
//
//
//    //获取打卡状态
//    public static String getClockStatus(RecordSearchVo record) {
//        String result = "";
//        //签到
//        if (record.getStartRecords().size() == 0) {
//            result += "缺卡";
//        } else {
//            long st = transformToTimeStamp(record.getCourseDate().toString() + " " + record.getStartTime().toString());//上课时间
//            long ct = transformToTimeStamp(record.getStartRecords().get(0).getTime().toString());//打卡时间
//            result += ct <= st ? "正常" : "迟到";
//        }
//        result += " / ";
//        //签退
//        if (record.getEndRecords().size() == 0) {
//            result += "缺卡";
//        } else {
//            long et = transformToTimeStamp(record.getCourseDate().toString() + " " + record.getEndTime().toString());//上课时间
//            long ct = transformToTimeStamp(record.getStartRecords().get(0).getTime().toString());//打卡时间
//            result += ct > et ? "正常" : "早退";
//        }
//        return result;
//    }
//
//    public static long transformToTimeStamp(String dateTime) {
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        try {
//            return Long.parseLong(String.valueOf(sdf.parse(dateTime).getTime() / 1000));
//        } catch (ParseException e) {
//            e.printStackTrace();
//        }
//        return 0;
//    }
//
//    //获取yyyy-MM-dd格式
//    public static String getyyyyMMddDate(Date date) {
//        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
//        return simpleDateFormat.format(date);
//    }
//
//    //获取yyyy-MM-dd HH:mm:ss格式
//    public static String getyyyyMMddHHmmssDate(Date date) {
//        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT+0"));
//        return simpleDateFormat.format(date);
//    }
//
//}
